import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd/message';
import { StaffsService } from 'src/app/shared/services/http/staffs.service';
import { CredentialsDto } from 'src/app/shared/services/http/dto/credentials.dto';

@Component({
  selector: 'app-create-staff-form',
  templateUrl: './create-staff-form.component.html',
  styleUrls: ['./create-staff-form.component.css'],
})
export class CreateStaffFormComponent implements OnInit {
  // 员工表单
  staffForm: FormGroup = this.formBuilder.group({
    account: [''],
    password: [''],
  });

  // 按钮是否被禁止
  disabled = false;

  // 创建成功事件
  @Output() create = new EventEmitter();

  constructor(
    private formBuilder: FormBuilder,
    private staffsService: StaffsService,
    private message: NzMessageService
  ) {}

  ngOnInit(): void {}

  /**
   * 提交创建员工
   * @param carwashDto 洗车点
   */
  onSubmit(credentialsDto: CredentialsDto) {
    this.disabled = true;
    this.staffsService.create(credentialsDto).subscribe(() => {
      this.message.create('success', `创建员工成功`);
      this.disabled = false;
      this.create.emit();
    });
  }
}
